package com.mapper;

import com.entity.Comments;
import com.entity.Lyrics;
import com.entity.Music;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper // 声明为 Mapper 配置文件
public interface MusicMapper {
    // 查询所有音乐
    // 先按英文歌曲排序，然后再按中文歌曲排序
//    @Select("SELECT\n" +
//            "\tid,\n" +
//            "\tsrc \n" +
//            "FROM\n" +
//            "\t(\n" +
//            "\t\t( SELECT id, src, 0 AS sort_order FROM songs WHERE en IS NOT NULL ) UNION ALL\n" +
//            "\t\t( SELECT id, src, 1 AS sort_order FROM songs WHERE en IS NULL ) \n" +
//            "\t) AS combined_results \n" +
//            "ORDER BY\n" +
//            "\tcombined_results.sort_order ASC,\n" +
//            "\tcombined_results.src ASC;")
    // 按编号默认顺序查询
    @Select("select id,src from songs order by id asc;")
    List<Music> selectAllSongs();

    // id 查询音乐
    @Select("select id,src from songs where id = #{id};")
    Music selectOneSongById(int id);

    // id 查询歌词
    @Select("select id,zh,en from songs where id = #{id};")
    Lyrics selectLyricsById(int id);

    // 新增评论
    @Insert("INSERT INTO comments (songId, commenterName, content, publishedAt) VALUES (#{songId}, #{commenterName}, #{content}, #{publishedAt});")
    int insertComments(Comments comments);

    // 查询评论信息
    @Select("select * from comments where songId = #{id} ORDER BY publishedAt DESC;")
    List<Comments> selectAllCommentsById(int id);
}
